<template>
  <div class="login">
    <van-form @submit="onSubmit" label-align="right" colon>
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px;">
        <van-button round block type="danger" native-type="submit">登录</van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      username: 'test',
      password: 'test',
    }
  },
  methods: {
    onSubmit(values) {
      // 触发 action 来实现用户登录，并保存用户登录状态数据
      // action 不能直接调用，需要 store.dispatch() 来触发
      this.$store.dispatch('user/loginAsync', values)
        .then(() => {
          // 登录成功后，向原期望访问页面跳转
          const route = this.$route.params.to || '/home'
          // 跳转
          this.$router.push(route)
        })
    },
  },
}
</script>

<style lang="less">

</style>
